package com.yanqu.road.logic.activity.chefduel;

import com.yanqu.road.dao.impl.activity.hotpotjump.CommonActivityConfigDaoImpl;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.autodata.chefduel.config.*;
import com.yanqu.road.entity.autodata.chefduel.entity.ChefDuel1DeviceGroup;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 厨神对决一阶段配置
 */
public class ChefDuel1Config {

    private ActivityInfo activityInfo;

    private int activityId;

    private int scoreId;            //积分道具

    private int energyItemId;       //体力道具

    private int initEnergy;         //初始体力

    private int maxEnergy;          //最大体力

    private int energyRecTime;      //回复一点体力时间

    private int tenDrawLimitTimes;  //十连需要抽几次

    private int endlessUnlockStage; //菊下楼对决--无尽（极限）关卡解锁条件

    private String resetSkillCost;

    private int calGradeParam1;

    private int calGradeParam2;

    private int firstDrawQualityIndex;

    //无尽模式最大客人数
    private int unLimitModeMaxGuest;
    //无尽模式最大
    private long unLimitModeMaxCoin;

    //抽取改造结果配置
    private Map<Integer, ChefDuel1DrawModifyConfig> drawModifyConfigMap = new ConcurrentHashMap<>();
    //抽取改造权重配置
    private Map<Integer,Integer> drawModifyWeightMap = new HashMap<>();
    //经验组抽取权重
    private Map<Integer,Integer> drawModifyGroupWeightMap = new HashMap<>();


    private Map<String, ActivityConfig> activityConfigMap = new ConcurrentHashMap<>();
    //设备配置
    private Map<Integer, ChefDuel1DeviceConfig> deviceConfigMap = new ConcurrentHashMap<>();
    //菜品配置
    private Map<Integer, ChefDuel1DishConfig> dishConfigMap = new ConcurrentHashMap<>();
    //顾客配置
    private Map<Integer, ChefDuel1GuestConfig> guestConfigMap = new ConcurrentHashMap<>();
    //等级升级配置
    private Map<Integer, ChefDuel1LevelConfig> levelConfigMap = new ConcurrentHashMap<>();
    //关卡配置
    private Map<Integer, ChefDuel1MissionConfig> missionConfigMap = new ConcurrentHashMap<>();
    //操作对象配置
    private Map<Integer, ChefDuel1ObjectConfig> objectConfigMap = new ConcurrentHashMap<>();
    //技能配置
    private Map<Integer,ChefDuel1TalentConfig> talentConfigMap = new ConcurrentHashMap<>();

    private Random random = new Random();

    private RandomHelper randomHelper = new RandomHelper();

    public ActivityInfo getActivityInfo() {
        return activityInfo;
    }

    public ChefDuel1Config (ActivityInfo tmpActivityInfo, Map<String, ActivityConfig> tmpActivityConfigMap){
        this.activityInfo = tmpActivityInfo;
        this.activityId = activityInfo.getActivityId();
        this.activityConfigMap = tmpActivityConfigMap;

        this.scoreId = tmpActivityConfigMap.get("EXCHANGE_SCORE_ITEM_ID").getIntValue();
        this.energyItemId = tmpActivityConfigMap.get("CHEFDUEL_ENERGY_ITEM_ID").getIntValue();
        this.initEnergy = tmpActivityConfigMap.get("CHEFDUEL_INITIAL_ENERGY").getIntValue();
        this.maxEnergy = tmpActivityConfigMap.get("CHEFDUEL_ENERGY_MAX").getIntValue();
        this.energyRecTime = tmpActivityConfigMap.get("CHEFDUEL_ENERGY_RECOVER").getIntValue();
        this.tenDrawLimitTimes = tmpActivityConfigMap.get("CHEFDUEL_ENERGY_TEN_CONSUME_LIMIT").getIntValue();

        List<Integer> chefduelEndlessMissionLimit = tmpActivityConfigMap.get("CHEFDUEL_ENDLESS_MISSION_LIMIT").getIntListValue("\\|");
        this.unLimitModeMaxGuest = chefduelEndlessMissionLimit.get(0);
        this.unLimitModeMaxCoin = chefduelEndlessMissionLimit.get(1);


        //300,10,600|500,20,300|800,40,80|1000,60,30|10000,200,1
        //菊下楼对决--经验抽取参数(经验值,积分值,权重|经验值,积分值,权重)
        String chefduelLottery = tmpActivityConfigMap.get("CHEFDUEL_LOTTERY").getValue();
        Map<Integer, ChefDuel1DrawModifyConfig> tmpDrawModifyConfigMap = new ConcurrentHashMap<>();
        Map<Integer,Integer> tmpDrawModifyWeightMap = new HashMap<>();
        int no = 0;
        List<String> lotteryList = StringUtils.stringToStringList(chefduelLottery, "\\|");
        for (String s : lotteryList) {
            no++;
            List<Integer> integers = StringUtils.stringToIntegerList(s, ",");
            ChefDuel1DrawModifyConfig drawModifyConfig = new ChefDuel1DrawModifyConfig(no,integers.get(0),integers.get(1),integers.get(2));
            tmpDrawModifyConfigMap.put(no,drawModifyConfig);
            tmpDrawModifyWeightMap.put(no,drawModifyConfig.getWeight());
        }
        this.drawModifyConfigMap = tmpDrawModifyConfigMap;
        this.drawModifyWeightMap = tmpDrawModifyWeightMap;

        //菊下楼对决--经验抽取设施组权重(设施组类型,权重|设施组类型,权重)1,100|2,100|3,100|4,100|5,100|6,100
        Map<Integer,Integer> tmpDrawModifyGroupWeightMap = new HashMap<>();
        String chefduelLotteryGroupWeight = tmpActivityConfigMap.get("CHEFDUEL_LOTTERY_WEIGHT").getValue();
        List<String> groupStrings = StringUtils.stringToStringList(chefduelLotteryGroupWeight, "\\|");
        for (String s : groupStrings) {
            List<Integer> integers = StringUtils.stringToIntegerList(s, ",");
            tmpDrawModifyGroupWeightMap.put(integers.get(0),integers.get(1));
        }
        this.drawModifyGroupWeightMap = tmpDrawModifyGroupWeightMap;

        this.resetSkillCost = tmpActivityConfigMap.get("CHEFDUEL_TALENT_RESET").getValue();

        //去数据库拿数据
        this.deviceConfigMap = new CommonActivityConfigDaoImpl<ChefDuel1DeviceConfig>().getConfigMap(ChefDuel1DeviceConfig.class,this.activityId);
        this.dishConfigMap = new CommonActivityConfigDaoImpl<ChefDuel1DishConfig>().getConfigMap(ChefDuel1DishConfig.class,this.activityId);
        this.guestConfigMap = new CommonActivityConfigDaoImpl<ChefDuel1GuestConfig>().getConfigMap(ChefDuel1GuestConfig.class,this.activityId);
        this.levelConfigMap = new CommonActivityConfigDaoImpl<ChefDuel1LevelConfig>().getConfigMap(ChefDuel1LevelConfig.class,this.activityId);
        this.missionConfigMap = new CommonActivityConfigDaoImpl<ChefDuel1MissionConfig>().getConfigMap(ChefDuel1MissionConfig.class,this.activityId);
        this.objectConfigMap = new CommonActivityConfigDaoImpl<ChefDuel1ObjectConfig>().getConfigMap(ChefDuel1ObjectConfig.class,this.activityId);
        this.talentConfigMap = new CommonActivityConfigDaoImpl<ChefDuel1TalentConfig>().getConfigMap(ChefDuel1TalentConfig.class,this.activityId);
        for (ChefDuel1TalentConfig talentConfig:talentConfigMap.values()){
            talentConfig.setUnlockTalentsList(StringUtils.stringToIntegerList(talentConfig.getUnlockTalents(),"\\|"));
        }

        List<Integer> chefduelScoreParam = tmpActivityConfigMap.get("CHEFDUEL_SCORE_PARAM").getIntListValue("\\|");
        this.calGradeParam1 = chefduelScoreParam.get(0);
        this.calGradeParam2 = chefduelScoreParam.get(1);

        this.endlessUnlockStage = tmpActivityConfigMap.get("CHEFDUEL_ENDLESS_MISSION_UNLOCK").getIntValue();
        this.firstDrawQualityIndex = tmpActivityConfigMap.get("CHEFDUEL_LOTTERY_INITIAL_PARAM").getIntValue();

    }

    /**
     * 随机一个改造奖励配置
     */
    public ChefDuel1DrawModifyConfig randomDrawModifyConfig(){
        int randomKey = randomHelper.getRandomKeyByWeightEx(drawModifyWeightMap);
        return drawModifyConfigMap.getOrDefault(randomKey, null);
    }

    /**
     * 获取第一抽必定出的品质
     */
    public ChefDuel1DrawModifyConfig getFirstDrawModifyConfig(){
        return drawModifyConfigMap.getOrDefault(getFirstDrawQualityIndex(), null);
    }

    /**
     * 随机一个设备组id
     * @return
     */
    public int randomDeviceGroupId(List<Integer> maxLevelId){
        //如果全满级 没有一个满级 或者null的情况下 全随机
        if (maxLevelId == null || maxLevelId.isEmpty() || maxLevelId.size() == drawModifyGroupWeightMap.size()){
            return randomHelper.getRandomKeyByWeightEx(drawModifyGroupWeightMap);
        }else {
            HashMap<Integer, Integer> noMaxLevelMap = new HashMap<>(drawModifyGroupWeightMap);
            for (int needRemove:maxLevelId){
                noMaxLevelMap.remove(needRemove);
            }
            return randomHelper.getRandomKeyByWeightEx(noMaxLevelMap);
        }
    }

    public Map<String, ActivityConfig> getActivityConfigMap() {
        return activityConfigMap;
    }

    public void setActivityConfigMap(Map<String, ActivityConfig> activityConfigMap) {
        this.activityConfigMap = activityConfigMap;
    }

    public void setActivityInfo(ActivityInfo activityInfo) {
        this.activityInfo = activityInfo;
    }

    public int getActivityId() {
        return activityId;
    }

    public void setActivityId(int activityId) {
        this.activityId = activityId;
    }

    public Random getRandom() {
        return random;
    }

    public void setRandom(Random random) {
        this.random = random;
    }

    public Map<Integer, ChefDuel1DeviceConfig> getDeviceConfigMap() {
        return deviceConfigMap;
    }

    public Map<Integer, ChefDuel1DishConfig> getDishConfigMap() {
        return dishConfigMap;
    }

    public Map<Integer, ChefDuel1GuestConfig> getGuestConfigMap() {
        return guestConfigMap;
    }

    public Map<Integer, ChefDuel1LevelConfig> getLevelConfigMap() {
        return levelConfigMap;
    }

    public Map<Integer, ChefDuel1MissionConfig> getMissionConfigMap() {
        return missionConfigMap;
    }

    public Map<Integer, ChefDuel1ObjectConfig> getObjectConfigMap() {
        return objectConfigMap;
    }

    public Map<Integer, ChefDuel1TalentConfig> getTalentConfigMap() {
        return talentConfigMap;
    }

    public int getScoreId() {
        return scoreId;
    }

    public int getEnergyItemId() {
        return energyItemId;
    }

    public int getInitEnergy() {
        return initEnergy;
    }

    public int getMaxEnergy() {
        return maxEnergy;
    }

    public int getEnergyRecTime() {
        return energyRecTime;
    }

    public int getTenDrawLimitTimes() {
        return tenDrawLimitTimes;
    }

    //通过关卡id获取关卡配置
    public ChefDuel1MissionConfig getMissionConfig(int missionId) {
        return missionConfigMap.getOrDefault(missionId, null);
    }


    //获取初始化设备组map
    public Map<Integer, ChefDuel1DeviceGroup> getInitDeviceDataMap(){
        Map<Integer, ChefDuel1DeviceGroup> initDeviceDataMap = new ConcurrentHashMap<>();
        for (int deviceGroupId:drawModifyGroupWeightMap.keySet()){
            ChefDuel1DeviceGroup deviceGroup = new ChefDuel1DeviceGroup();
            deviceGroup.setId(deviceGroupId);
            deviceGroup.setExp(0);
            deviceGroup.setLevel(1);
            deviceGroup.setTotalPoint(0);
            initDeviceDataMap.put(deviceGroupId, deviceGroup);
        }
        return initDeviceDataMap;
    }

    public int getEndlessUnlockStage() {
        return endlessUnlockStage;
    }

    public String getResetSkillCost() {
        return resetSkillCost;
    }

    public Map<Integer, ChefDuel1DrawModifyConfig> getDrawModifyConfigMap() {
        return drawModifyConfigMap;
    }

    public Map<Integer, Integer> getDrawModifyWeightMap() {
        return drawModifyWeightMap;
    }

    public Map<Integer, Integer> getDrawModifyGroupWeightMap() {
        return drawModifyGroupWeightMap;
    }

    public ChefDuel1LevelConfig getLevelConfig(int oldLv){
        return levelConfigMap.get(oldLv);
    }

    public ChefDuel1TalentConfig getTalentConfig(int skillId){
        return talentConfigMap.get(skillId);
    }

    public int getCalGradeParam1() {
        return calGradeParam1;
    }

    public int getCalGradeParam2() {
        return calGradeParam2;
    }

    public RandomHelper getRandomHelper() {
        return randomHelper;
    }

    public int getFirstDrawQualityIndex() {
        return firstDrawQualityIndex;
    }

    public int getUnLimitModeMaxGuest() {
        return unLimitModeMaxGuest;
    }

    public long getUnLimitModeMaxCoin() {
        return unLimitModeMaxCoin;
    }
}
